﻿@page "/listbox"

@using NorthwindBlazor.Data
@using NorthwindBlazor.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<RadzenExample Name="ListBox">
<div class="row">
    <div class="col-md-4">
        <h3>ListBox</h3>
        <RadzenListBox @bind-Value="@value" Data="@customers" TextProperty="CompanyName" ValueProperty="CustomerID" Style="margin-bottom: 20px;height:200px;" Change="@(args => Change(args, "ListBox"))" />
        <br />
        <h3>ListBox with template</h3>
        <RadzenListBox @bind-Value="value" Data="@customers" TextProperty="CompanyName" ValueProperty="CustomerID"
                        Change="@(args => Change(args, "ListBox with template"))" Style="margin-bottom: 20px; height:200px;">
            <Template>
                Company: @((context as Customer).CompanyName)
            </Template>
        </RadzenListBox>
    </div>
    <div class="col-md-4">
        <h3>ListBox with filtering</h3>
        <RadzenListBox AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" @bind-Value="value" Data="@customers" TextProperty="CompanyName" ValueProperty="CustomerID"
                        Change="@(args => Change(args, "ListBox with filtering"))" Style="margin-bottom: 20px; height:200px;" />
        <br />
        <h3>ListBox with multiple selection</h3>
        <RadzenListBox AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" @bind-Value="multipleValues" Multiple="true" Data="@customers" TextProperty="CompanyName" ValueProperty="CustomerID"
                        Change="@(args => Change(args, "ListBox with multiple selection"))" Style="margin-bottom: 20px; height:200px;" />
    </div>
    <div class="col-md-4">
        <h3>Events</h3>
        <RadzenCard style="overflow: auto;height:500px;">
            @foreach (var e in events.OrderByDescending(i => i.Key))
            {
                @e.Value
                <br />
            }
        </RadzenCard>
    </div>
</div>
</RadzenExample>

@code {
    IEnumerable<string> multipleValues = new string[] { "ALFKI", "ANATR" };
    string value = "ALFKI";

    IEnumerable<Customer> customers;

    protected override async Task OnInitializedAsync()
    {
        customers = await Task.FromResult(dbContext.Customers);
    }

    Dictionary<DateTime, string> events = new Dictionary<DateTime, string>();

    void Change(object value, string name)
    {
        var str = value is IEnumerable<object> ? string.Join(", ", (IEnumerable<object>)value) : value;

        events.Add(DateTime.Now, $"{name} value changed to {str}");
        StateHasChanged();
    }
}
